package com.wanban.downloader.utils

import android.text.TextUtils
import com.arialyy.aria.core.Aria
import com.arialyy.aria.core.common.HttpOption
import com.arialyy.aria.core.download.DownloadTaskListener
import com.arialyy.aria.core.task.DownloadTask
import com.wanban.downloader.db.DownloadEntity
import com.wanban.downloader.listener.OnBaseDownloadListener
import com.wanban.downloader.listener.OnVideoDownloadListener
import com.wanban.downloader.model.M3U8Seg


class Mp4DownloadUtils : OnBaseDownloadListener, DownloadTaskListener {

    private lateinit var parentUrl: String
    private var mp4Segment: M3U8Seg? = null
    private var taskId = -1L
    private var listener: OnVideoDownloadListener? = null
    fun download(task: DownloadEntity, callback: OnVideoDownloadListener) {
        parentUrl = task.url
        mp4Segment = task.m3u8?.tsList?.firstOrNull()
        if (mp4Segment == null) {
            callback.onError(task.url, "找不到下载内容")
            return
        }
        val filePath = FileDownloadUtils.getMp4FilePath(task.getFileName())
        if (TextUtils.isEmpty(filePath)) {
            callback.onError(task.url, "mp4目录创建失败")
            return
        }
        listener = callback


        Aria.download(this).register()

        // 先尝试取记录中的下载
        val addTask = Aria.download(this).getDownloadEntity(mp4Segment!!.url)?.firstOrNull()
        if (addTask != null) {
            taskId = addTask.id
        }
        if (taskId < 0) {
            val option = HttpOption()
            if (task.header != null) {
                option.addHeaders(task.header)
            }
            taskId = Aria.download(this)
                .load(mp4Segment!!.url) //读取下载地址
                .setFilePath(filePath) //设置文件保存的完整路径
                .option(option)
                .ignoreCheckPermissions()
                .create() //创建并启动下载
        } else {
            Aria.download(this)
                .load(taskId)     //读取任务id
                .resume()
        }

    }
    


    override fun cancelAllTask() {
        if (taskId < 0) return
        Aria.download(this).load(taskId).stop()
    }

    override fun delete() {
        if (taskId < 0) return
        Aria.download(this).load(taskId).cancel()
    }


    override fun onWait(task: DownloadTask) {
        
    }

    override fun onPre(task: DownloadTask) {
        
    }

    override fun onTaskPre(task: DownloadTask) {
        
    }

    override fun onTaskResume(task: DownloadTask) {
        
    }

    override fun onTaskStart(task: DownloadTask) {
        if(task.key != mp4Segment?.url) return
        //在这里处理任务完成的状态
        listener?.onStart(parentUrl)
    }

    override fun onTaskStop(task: DownloadTask) {
        
    }

    override fun onTaskCancel(task: DownloadTask) {
        
    }

    override fun onTaskFail(task: DownloadTask, e: Exception?) {
        //在这里处理任务完成的状态
        if(task.key != mp4Segment?.url) return
        listener?.onError(parentUrl, e?.message?:"")
    }

    override fun onTaskComplete(task: DownloadTask) {
        //在这里处理任务完成的状态
        if(task.key != mp4Segment?.url) return
        listener?.onSuccess(parentUrl)
    }

    override fun onTaskRunning(task: DownloadTask) {
        if(task.key != mp4Segment?.url) return
        listener?.onProgress(parentUrl, task.speed, task.percent.toFloat(), task.currentProgress)
    }

    override fun onNoSupportBreakPoint(task: DownloadTask) {
        
    }

}

